package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.pojo.Dept;
import com.example.demo.pojo.Member;
import com.example.demo.service.DeptService;
import com.example.demo.service.MemberService;
import com.example.demo.utils.SearchDo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Resource
    private MemberService memberService;

    @Resource
    private DeptService deptService;

    @RequestMapping("/list")
    public String getMemberList(Integer page, Integer pageSize ,SearchDo searchDo){
        JSONObject object = new JSONObject();
        PageInfo<Member> list = memberService.queryMemberByPage(page, pageSize ,searchDo);
        //查询所有机构信息
        List<Dept> depts = deptService.selectAllDept("");
        ArrayList<Member> result = new ArrayList<>();
        Map<BigDecimal, String> map = new HashMap<>();
        for (Dept dept:depts) {
            map.put(dept.getId(),dept.getName());
        }
        for (Member member:list.getList()) {
            member.setDeptName(map.get(member.getDeptid()));
            result.add(member);
        }
        object.put("Rows",result);
        object.put("Total",list.getTotal());
        return object.toJSONString();
    }

}
